<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary" @click="addFeeRule">增加停车计费规则</el-button>
      
      <el-button @click="exportToExcel">导出Excel</el-button>

    </div>

    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" label="序号" :index="indexMethod"/>
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上限(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式">
          <template #default="scope">
            {{ formatChargeType(scope.row.chargeType) }}
          </template>

        </el-table-column>

        <el-table-column label="计费规则" prop="ruleNameView"/>
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text" @click="toEditRule(scope.row)">编辑</el-button>

            <el-button size="mini" type="text" @click="delFeeRule(scope.row.id)">删除</el-button>

          </template>

        </el-table-column>

      </el-table>
      <div class="page-container">
      <!-- total和page-size是必须要配置的，总页数=total/page-size
      @current-change必须要配置的，切换页码用 -->
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :page-size="params.pageSize"
        @current-change="handleChange"
      />
    </div>

    </div>
    <AddRule 
    :dialog-visible.sync="dialogVisible"
    :edit-data="editForm" 
    @getList="getRuleList"></AddRule>
  </div>

</template>

<script>
import { getRuleListAPI, delFeeRuleAPI } from '@/api/rule'
import { utils, writeFileXLSX } from 'xlsx'
import AddRule from './components/AddRule.vue'
export default {
  name: 'Building',
  components: {
    AddRule
  },
  data() {
    return {
      ruleList: [],
      params: {
        page: 1,
        pageSize: 2
      },
      total: 0,
      dialogVisible: false,
      editForm: null
    }
  },
  created() {
    this.getRuleList()
  },
  methods: {
    //编辑计费规则
    toEditRule(row) {
      //console.log(row)
      this.dialogVisible = true
      // const {id,ruleNumber,ruleName,chargeType,chargeCeiling,freeDuration,durationTime,durationPrice,durationType,turnPrice,partitionFrameTime,partitionFramePrice,partitionIncreaseTime,partitionIncreasePrice } = row
      // this.addForm = {id,ruleNumber,ruleName,chargeType,chargeCeiling,freeDuration,durationTime,durationPrice,durationType,turnPrice,partitionFrameTime,partitionFramePrice,partitionIncreaseTime,partitionIncreasePrice }
      //构造完整字段
      this.editForm = {
        id: row.id,
        ruleNumber: row.ruleNumber,
        ruleName: row.ruleName,
        chargeType: row.freeDuration,
        freeDuration: row.freeDuration,
        chargeCeiling: row.chargeCeiling,
        // 根据计费方式补充对应字段
        durationTime: row.durationTime,
        durationPrice: row.durationPrice,
        turnPrice: row.turnPrice,
        partitionFrameTime: row.partitionFrameTime,
        partitionFramePrice: row.partitionFramePrice,
        partitionIncreaseTime: row.partitionIncreaseTime,
        partitionIncreasePrice: row.partitionIncreasePrice
      }
    },
    //删除计费规则
    delFeeRule(id) {
      this.$confirm('确认删除吗？', '温馨提示').then(async() => {
        // console.log(id)
        await delFeeRuleAPI(id)
        this.$message.success('删除成功')
        this.getRuleList()
      }).catch(() => {})
    },
    //控制弹框显示隐藏
    addFeeRule() {
      this.dialogVisible = true
    },
    //适配收费状态，格式化收费类型
    formatChargeType(type) {
      const TYPEMAP = {
        'duration': '按时长收费',
        'turn': '按次收费',
        'partition':'分段收费'
      }
      return TYPEMAP[type]
    },
    //导出工作表
    async exportToExcel() {
      //获取要导出的业务数据
      const res = await getRuleListAPI(this.params)
      //要导出的字段名
      const tableHeaderKeys = ['ruleNumber', 'ruleName', 'freeDuration', 'chargeCeiling', 'chargeType', 'ruleNameView']
      //要导出的表头
      const tableHeaderValues = ['计费规则编号', '计费规则名称', '免费时长(分钟)', '收费上线(元)', '计费方式', '计费规则']
      // 以excel表格的顺序调整后端数据
      const sheetData = res.data.rows.map((item) => {
        const obj = {}
        tableHeaderKeys.forEach(key => {
          //赋值，针对计费规则做处理
          if (key === 'chargeType') {
            obj[key] = this.formatChargeType(item[key])
          } else {
            obj[key] = item[key]
          }
        })
        return obj
      })
      //创建一个新的工作簿
      const workbook = utils.book_new()
      //创建一个工作表，要求一个对象数组格式
      const worksheet = utils.json_to_sheet(sheetData)
      //把工作表添加到工作簿，Data为工作表名称
      utils.book_append_sheet(workbook, worksheet, 'Data')
      // 改写表头
      utils.sheet_add_aoa(worksheet, [tableHeaderValues], { origin: 'A1' })
      // 导出方法进行导出
      writeFileXLSX(workbook, 'SheetJSVueAoO.xlsx')
    },
    // 计算序号
    indexMethod(index) {
      return (this.params.page - 1) * this.params.pageSize + index + 1
    },
    //分页，val是点击的不同的页码值
    handleChange(val) {
      this.params.page = val 
      this.getRuleList()
    },
    //调获取规则列表接口
    async getRuleList() {
      const res = await getRuleListAPI(this.params)
      //console.log(res)
      this.ruleList = res.data.rows
      this.total = res.data.total
    }
  }
}
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}

</style>

